var listPage = avalon.define({
	$id: "listPage",
	uid: "",
	ids: [],
	receiptedPageNo: 1,
	unReceiptedPageNo: 1,
	noData: "",
	receipted: [],
	unReceipted: [],
	noReceiptedData: "",
	noUnReceiptedData: "",
	flag: 0,
	receiptedTotal: 0
});

mui.ready(function() {
	
	var filterCanvasWrapper = mui('#filterCanvasWrapper');
	mui(".header").on('tap', '.searchBtn', function() {
		document.activeElement.blur();
		filterCanvasWrapper.offCanvas('show');
	});
	var height = document.documentElement.clientHeight || document.body.clientHeight;
	document.getElementById("filterCanvasWrapper").style.minHeight = height +"px";
	mui('.mui-scroll-wrapper').scroll();
	
	listPage.uid = $('#uid').val();
	$("input[name='selectAll']").removeAttr("checked");
	listPage.flag = $('input:radio:checked').val();
	var mui_receipted = mui('#receipted');
	var mui_unReceipted = mui('#unReceipted');

	mui_receipted.pullRefresh({
		container: "#receipted",
		down: {
			auto: true,
			callback: function() {
				listPage.receiptedPageNo = 1;
				listPage.receipted = [];
				listPage.noReceiptedData = "";
				var po = $("#po").val();
				var ci = $("#ci").val();
				var norm = $("#norm").val();
				var startDate = $("#startDate").val();
				var endDate = $("#endDate").val();
				var self = this;
				setTimeout(function() {
					mui.ajax("/order/orderlist.json", {
						data: {
							"pageNo": 1,
							"uid": listPage.uid,
							"status": 1,
							"type":listPage.flag,
							"po":po,
							"ci":ci,
							"norm":norm,
							"startDate":startDate,
							"endDate":endDate
						},
						success: function(data) {
							var records = data.records;
							listPage.receiptedTotal = data.totalCount;
							if(null == records || records.length == 0) {
								listPage.receipted = [];
								listPage.noReceiptedData = "<img src='../../resources/images/noDataImg.png'><p>No data</p>";
							} else {
								listPage.receipted = data.records;
								listPage.noReceiptedData = "";
								listPage.receiptedPageNo = 2;
							}
						}
					});
					self.endPulldownToRefresh(false);
				}, 500);
			}
		},
		up: {
			callback: function() {
				var self = this;
				setTimeout(function() {
					listPage.noReceiptedData = "";
					var po = $("#po").val();
					var ci = $("#ci").val();
					var norm = $("#norm").val();
					var startDate = $("#startDate").val();
					var endDate = $("#endDate").val();
					mui.ajax("/order/orderlist.json", {
						data: {
							"pageNo": listPage.receiptedPageNo,
							"uid": listPage.uid,
							"status": 1,
							"type":listPage.flag,
							"po":po,
							"ci":ci,
							"norm":norm,
							"startDate":startDate,
							"endDate":endDate
						},
						success: function(data) {
							var records = data.records;
							listPage.receiptedTotal = data.totalCount;
							if((listPage.receiptedPageNo <= data.totalPage) && (null != records)) {
								listPage.receipted = listPage.receipted.concat(records);
								listPage.receiptedPageNo++;
							} else {
								mui.toast("No More Data");
							}
							if(data.totalCount == 0) {
								listPage.noReceiptedData = "<img src='../../resources/images/noDataImg.png'><p>No data</p>";
							} else {
								listPage.noReceiptedData = "";
							}
						}
					});
					self.endPullupToRefresh(false);
				}, 1000);
			}
		}

	});

	mui_unReceipted.pullRefresh({
		container: "#unReceipted",
		down: {
			auto: true,
			callback: function() {
				listPage.unReceiptedPageNo = 1;
				listPage.unReceipted = [];
				listPage.noUnReceiptedData = "";
				var po = $("#po").val();
				var ci = $("#ci").val();
				var norm = $("#norm").val();
				var startDate = $("#startDate").val();
				var endDate = $("#endDate").val();
				var self = this;
				setTimeout(function() {
					mui.ajax("/order/orderlist.json", {
						data: {
							"pageNo": 1,
							"uid": listPage.uid,
							"status": 0,
							"type":listPage.flag,
							"po":po,
							"ci":ci,
							"norm":norm,
							"startDate":startDate,
							"endDate":endDate
						},
						success: function(data) {
							var records = data.records;
							if(null == records || records.length == 0) {
								listPage.unReceipted = [];
								listPage.noUnReceiptedData = "<img src='../../resources/images/noDataImg.png'><p>No data</p>";
							} else {
								listPage.unReceipted = data.records;
								listPage.noUnReceiptedData = "";
								listPage.unReceiptedPageNo = 2;
							}
						}
					});
					self.endPulldownToRefresh(false);
				}, 500);
			}
		},
		up: {
			callback: function() {
				var self = this;
				setTimeout(function() {
					listPage.noUnReceiptedData = "";
					var po = $("#po").val();
					var ci = $("#ci").val();
					var norm = $("#norm").val();
					var startDate = $("#startDate").val();
					var endDate = $("#endDate").val();
					mui.ajax("/order/orderlist.json", {
						data: {
							"pageNo": listPage.unReceiptedPageNo,
							"uid": listPage.uid,
							"status": 0,
							"type":listPage.flag,
							"po":po,
							"ci":ci,
							"norm":norm,
							"startDate":startDate,
							"endDate":endDate
						},
						success: function(data) {
							var records = data.records;
							if((listPage.unReceiptedPageNo <= data.totalPage) && (null != records)) {
								listPage.unReceipted = listPage.unReceipted.concat(records);
								listPage.unReceiptedPageNo++;
							} else {
								mui.toast("No More Data");
							}
							if(data.totalCount == 0) {
								listPage.noUnReceiptedData = "<img src='../../resources/images/noDataImg.png'><p>No data</p>";
							} else {
								listPage.noUnReceiptedData = "";
							}
						}
					});
					self.endPullupToRefresh(false);
				}, 1000);
			}
		}
	});

	mui(".mui-radio").on('change', "input[type='radio']", function() {
		listPage.flag = $('input:radio:checked').val();
		mui('#receipted').pullRefresh().pulldownLoading();
		mui('#unReceipted').pullRefresh().pulldownLoading();
		$("input[name='selectAll']").removeAttr("checked");
	});

	mui(".order-selected").on('change', "input[name='selectAll']", function() {
		if(this.checked) {
			$("input[name='signs']").each(function() {
				this.checked = true;
			});
		} else {
			$("input[name='signs']").not(':disabled').each(function() {
				this.checked = false;
			});
		}
	});

	mui(".mui-scroll").on('tap', '.open-pdpUNReceipted', function() {
		var po = this.getAttribute('po');
		var ci = this.getAttribute("ci");
		var outDate = this.getAttribute("outDate");
		var type = listPage.flag;
		var url = "";
		if(type == 'po') {
			url = "pdp.html" + "?type=" + type + "&po=" + po + "&outDate=" + outDate 
		}else{
			url = "pdp.html" + "?type=" + type + "&ci=" + ci + "&outDate=" + outDate 
		}
		mui.openWindow({
			url: url
		});
	});

	mui(".mui-scroll").on('tap', '.open-pdpReceipted', function() {
		var po = this.getAttribute('po');
		var ci = this.getAttribute("ci");
		var outDate = this.getAttribute("outDate");
		var type = listPage.flag;
		var url = "";
		if(type == 'po') {
			url = "pdp.html" + "?type=" + type + "&isReceipted=1" + "&po=" + po + "&outDate=" + outDate 
		}else{
			url = "pdp.html" + "?type=" + type + "&isReceipted=1" + "&ci=" + ci + "&outDate=" + outDate 
		}
		mui.openWindow({
			url: url 
		});
	});

	//阻止chekcbox点击事件冒泡
	mui(".mui-scroll").on('tap', 'input[name="signs"]', function(e) {
		if(this.checked) {
			this.checked = false;
		}else{
			this.checked = true;
		}
		return false;
	});
	
	//reset按钮
	mui(".search-btn").on('tap', '.resetBtn', function() {
		$("input[type='text']").each(function(){
			$(this).val("").blur();
		});
	});
	//搜索按钮
	mui(".search-btn").on('tap', '.searchBtn', function() {
		
		mui('.mui-off-canvas-wrap').offCanvas('show');
		mui('#receipted').pullRefresh().pulldownLoading();
		mui('#unReceipted').pullRefresh().pulldownLoading();

	});
	
	
	mui(".receipt").on('tap', 'a', function() {
		var type = listPage.flag;
		$.each($('input[name="signs"]:checked').not(':disabled'), function() {
			var param = "";
			if(type == 'po') {
				param = $(this).attr("po")+"#"+$(this).attr("outDate")
			}else{
				param = $(this).attr("ci")+"#"+$(this).attr("outDate")
			}
			listPage.ids.push(param);
		});
		if(listPage.ids.length == 0) {
			mui.alert("Please Select Order");
		} else {
			$(".receipt-confirm").show();
		}
	});

	mui(".mui-popup-buttons").on('tap', '.cancel', function() {
		$(".receipt-confirm").hide();
	});

	mui(".mui-popup-buttons").on('tap', '.ok', function() {
		mui.ajax("/order/signOrder.do", {
			data: {
				"type":listPage.flag,
				"ids": listPage.ids
			},
			success: function(data) {
				$(".receipt-confirm").hide();
				mui.alert(data.msg);
				listPage.ids = [];
				mui_unReceipted.pullRefresh().pulldownLoading();
				mui_receipted.pullRefresh().pulldownLoading();
			}
		});
	});
	
	document.getElementById("backdrop").addEventListener('tap', function() {
        document.activeElement.blur();
    });
    
    mui(".mui-row").on('tap', '#showStatement', function() {
		$("#filterCanvasWrapper").addClass("mui-hidden");
		$("#statementPage").removeClass("mui-hidden");
	});
	
	mui(".header").on('tap', '#backReg', function() {
		$("#statementPage").addClass("mui-hidden");
		$("#filterCanvasWrapper").removeClass("mui-hidden");
	});
	
});